//
//  StatusCellView.swift
//  weibo
//
//  Created by Qing on 2017/7/26.
//  Copyright © 2017年 成哥哥. All rights reserved.
//

import UIKit


/// 距离边距
let StatusCellMargin: CGFloat = 10

/// 头像大小
let StatusCellIconWidth: CGFloat = 35

/// vip图标的大小
let StatusCellVerifiedIcom: CGFloat = 15


class StatusCellView: UITableViewCell {
    
//    微博视图模型
    var viewModel: StatusViewModel? {
        
        didSet{
//            设计工作
            topView.viewModel = viewModel
            contentLable.text = viewModel?.status.text
            
            
            pictrueView.viewModel = viewModel
            
            pictrueView.snp.updateConstraints { (make) in
//                make.height.equalTo(Int((arc4random() % 4) * 40 ))
                print("\(viewModel!.status.user?.screen_name)配图视图大小\(pictrueView.bounds)")
                make.height.equalTo(pictrueView.bounds.height)
                make.width.equalTo(pictrueView.bounds.width)
                
            }
        }
    }
    
    /// 根据指定的视图模型计算行高
    ///
    /// - Parameter vm: 视图模型
    /// - Returns: 返回视图模型对应的行高
    func rowHeight(vm: StatusViewModel) -> CGFloat {
    
        //1.记录视图模型 - 会调用上面的 didSet 设置内容及更新约束
        viewModel = vm
        
        //2.强制更新所有约束 -> 所有控件的frame会被正确计算
        layoutIfNeeded()
        
        //3.返回底部视图最大的高度（Y值）
        return bottomView.frame.maxY
    }
    
    
//MARK:  - 构造函数，初始化
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

        //MARK: - 懒加载控件
        //底部视图
        lazy var topView: StatusCellTopView = StatusCellTopView()
        //内容视图
        lazy var contentLable: UILabel = UILabel(texts: "爱上微博", fontSize: 12, ScreenInset: StatusCellMargin)
        //发布内容的略缩图
        lazy var pictrueView: StatusPictureView = StatusPictureView()
        //底部视图
        lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    
//        lazy var topSegView: UIView = UIView()
    
}

//MARK: - 界面设置
extension StatusCellView {

    
    /// 加载控件
    fileprivate func setUI(){
//        1.添加控件
//        addSubview(topView)
        self.contentView.addSubview(topView)
//        topView.backgroundColor = UIColor(white: 0.95, alpha: 1)
//        addSubview(contentLable)
        self.contentView.addSubview(contentLable)
        contentLable.backgroundColor = UIColor(white: 0.85, alpha: 1)
        self.contentView.addSubview(pictrueView)
        self.contentView.addSubview(bottomView)
        bottomView.backgroundColor = UIColor(white: 0.95, alpha: 1)
//        bottomView.backgroundColor = UIColor.brown
//        addSubview(topSegView)
//        topSegView.backgroundColor = UIColor.clear
        
//        2.自动而局
//        1>顶部视图
        topView.snp.makeConstraints { (make) in
            make.top.equalTo(contentView.snp.top)
            make.left.equalTo(contentView.snp.left)
            make.right.equalTo(contentView.snp.right)
            make.height.equalTo(StatusCellMargin + StatusCellIconWidth)
        }
        
//        2>内容视图
        contentLable.snp.makeConstraints { (make) in
            make.top.equalTo(topView.snp.bottom).offset(2 * StatusCellMargin)
            make.left.equalTo(contentView.snp.left).offset(StatusCellMargin)
//            make.bottom.equalTo(contentView.snp.bottom).offset(-StatusCellMargin)
        }
        
//        3>略缩图视图
        pictrueView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLable.snp.bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLable.snp.left)
//            make.width.equalTo(contentView.snp.width).offset(-2 * StatusCellMargin)
            make.width.equalTo(300)
            make.height.equalTo(90)
        }
        
//        4>底部视图
        bottomView.snp.makeConstraints { (make) in
            make.top.equalTo(pictrueView.snp.bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp.left)
            make.right.equalTo(self.snp.right)
            make.height.equalTo(44)
//          添加向上压的约束
//            make.bottom.equalTo(contentView.snp.bottom)
            
        }
        
//        topSegView.snp.makeConstraints { (make) in
//            make.bottom.equalTo(topView.snp.top)
//            make.left.equalTo(self.snp.left)
//            make.right.equalTo(self.snp.right)
//            make.height.equalTo(20)
//        }
    }
}
